#!/bin/bash
#####################################
#   点阅频道书籍定时校验   
#   yanbingquan
######################################
#function


############################
## 记录日志
## eg: logger "这是一个日志"
############################
function logger ()
{
   dataTime=`date "+%Y-%m-%d %H:%M:%S" `
   echo "$dataTime ######  $1 " >> Runlog.txt
}

############################
### 方法简要说明：
### 1. 是先查找一个字符串：带双引号的key。如果没找到，则直接返回defaultValue。
### 2. 查找最近的冒号，找到后认为值的部分开始了，直到在层数上等于0时找到这3个字符：,}]。
### 3. 如果有多个同名key，则依次全部打印（不论层级，只按出现顺序）
### @author lux feary
###
### 3 params: json, key, defaultValue
############################
function getJsonValuesByAwk() {
    awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
        foundKeyCount = 0
        while (length(json) > 0) {
            # pos = index(json, "\""key"\""); ## 这行更快一些，但是如果有value是字符串，且刚好与要查找的key相同，会被误认为是key而导致值获取错误
            pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*");
            if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;}

            ++foundKeyCount;
            start = 0; stop = 0; layer = 0;
            for (i = pos + length(key) + 1; i <= length(json); ++i) {
                lastChar = substr(json, i - 1, 1)
                currChar = substr(json, i, 1)

                if (start <= 0) {
                    if (lastChar == ":") {
                        start = currChar == " " ? i + 1: i;
                        if (currChar == "{" || currChar == "[") {
                            layer = 1;
                        }
                    }
                } else {
                    if (currChar == "{" || currChar == "[") {
                        ++layer;
                    }
                    if (currChar == "}" || currChar == "]") {
                        --layer;
                    }
                    if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) {
                        stop = currChar == "," ? i : i + 1 + layer;
                        break;
                    }
                }
            }

            if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) {
                if (foundKeyCount == 0) {print defaultValue;} exit 0;
            } else {
                print substr(json, start, stop - start);
            }

            json = substr(json, stop + 1, length(json) - stop)
        }
    }'
}
######################################
   
   env=xxxxxxx
   #env=xxxxxxxxx
   arrayBook=()
   bookCount=0
   ##
   #根据channels拿到modules
   ##
   echo "————------------——————————————————————————————————————————————————————————————————\n" >> Runlog.txt
   echo "———————————————ONE-循环校验———`date "+%Y-%m-%d %H:%M:%S"`—————————————————————————————————\n" >> Runlog.txt
   echo "————------------——————————————————————————————————————————————————————————————————\n" >> Runlog.txt
   channels="curl  -H 'appid: 101' -H 'terminal: aphone' -H 'version: 0.5.1' -H 'market: offical' -H 'model: OPPO+A37m' -H 'os: 5.1' -H 'sdk: 22'  -H 'User-Agent: okhttp/3.11.0' --compressed  \"http://$env/v1/bookstore/channels?auid=5ab14cc165b4f075b5014001f6010f9f&uid=0&ver=0.5.1&pl=1&ts=1560758432104&chl=offical&tk=&_r=120444216507&sig=1f6917d63417a6ffc23906f3e1f155be\""
   channelsResult=`eval $channels`
   logger "channelResult:${channelsResult}" 
   lengthChannels=`echo $channelsResult|jq '.data.channels|length'`
   for(( i=0;i<$lengthChannels;i++ ));
   do
        #####
	#根据modules拿到bookid
	#####
   	channelsName=`echo $channelsResult|jq .data.channels[$i].name`
   	channelsModules=`echo $channelsResult|jq .data.channels[$i].modules`
        mids=`echo $channelsModules|sed 's/[[:space:]]//g'|sed 's/\[//g'|sed 's/\]//g'|sed 's/\,/\%2C/g'`
      	logger "mids:$mids" 
        modulesUrl="curl -H 'appid: 101' -H 'terminal: aphone' -H 'version: 0.5.1' -H 'market: offical' -H 'model: OPPO+A37m' -H 'os: 5.1' -H 'sdk: 22' -H 'Host: test.apiscan.dyreader.cn' -H 'User-Agent: okhttp/3.11.0' --compressed \"http://$env/v1/bookstore/modules?mids=$mids&refresh_cnt=1&auid=5ab14cc165b4f075b5014001f6010f9f&uid=0&ver=0.5.1&pl=1&ts=1560761796937&chl=offical&tk=&_r=921032891102&sig=6580aef1e093363dbb89f5859f827ad3\""
        modulesResult=`eval $modulesUrl`
        #bookIds=`echo $modulesResult |jq '.data.modules[].data[].items[].bookId'`
        #lengthmodules=`echo $modulesResult |tr '\r\n' ' '|jq '.data.modules|length'`
        #for(( j=0;j<$lengthmodules;j++ ));
        #do
            #bookIds=`echo $modulesResult |tr '\r\n' ' '|jq  ".data.modules[$j].data[].items[].cover"|awk -F '_' '{print $1}'|awk -F '/' '{print $NF}'`
            bookIds=`getJsonValuesByAwk "$modulesResult" "bookId" "defaultValue"`
            #modulesTitle=`echo $modulesResult |tr '\r\n' ' '|jq  ".data.modules[$j].title"`
            for bookId in $bookIds
            do
                  let bookCount=bookCount+1
                  #根据bookId获取书籍名称，source来源。
                  detailUrl="curl -H 'appid: 101' -H 'terminal: aphone' -H 'version: 0.5.1' -H 'market: offical' -H 'model: OPPO+A37m' -H 'os: 5.1' -H 'sdk: 22' -H 'Host: test.apiscan.dyreader.cn' -H 'User-Agent: okhttp/3.11.0' --compressed \"http://$env/v1/book/detail?book=$bookId&auid=5ab14cc165b4f075b5014001f6010f9f&uid=0&ver=0.5.1&pl=1&ts=1560838608432&chl=offical&tk=&_r=417456766715&sig=122d7554e36500446ffbfbb39c1566ec\""
	          sourU=`eval $detailUrl`
                  sourUr=`echo $sourU|tr '\r\n' ' '|sed 's/0.0/0/g'`
           	  sourId=`echo $sourUr|jq '.data.sources[].id'|sed 's/\"//g'`
           	  sourName=`echo $sourUr|jq '.data.sources[].name'|sed 's/\"//g'`
	    	  bookName=`echo $sourUr|jq '.data.name'`
		  logger "bookName;$bookName,bookId:$bookId,sourceId:$sourId,modulesTitle:$modulesTitle,channelsName:$channelsName"

           	  #根据bookId,sourceId获取章节id
	     	  chapterUrl="curl -H 'appid: 101' -H 'terminal: aphone' -H 'version: 0.5.1' -H 'market: offical' -H 'model: OPPO+A37m' -H 'os: 5.1' -H 'sdk: 22'  -H 'Host: test.apiscan.dyreader.cn' -H 'User-Agent: okhttp/3.11.0' --compressed \"http://$env/v1/book/chapters?book=$bookId&source=$sourId&auid=5ab14cc165b4f075b5014001f6010f9f&uid=0&ver=0.5.1&pl=1&ts=1560829696572&chl=offical&tk=&_r=244928186669&sig=309aa72a8a22663c4ea02c19f899abff\""
	          chapterResult=`eval $chapterUrl` 
	          chapterId=`echo $chapterResult|tr '\r\n' ' '|jq '.data.volumes[0].chapters[0].id'|sed 's/\"//g'` 
                  volumesLength=`echo $chapterResult|tr '\r\n' ' '|jq '.data.volumes|length'`
                  let volumesLength=volumesLength-1
	          chapterId=`echo $chapterResult|tr '\r\n' ' '|jq ".data.volumes[$volumesLength].chapters[0].id"|sed 's/\"//g'` 
                  if [[ $chapterId == null ]];then
                        chapterId=`echo $chapterResult|tr '\r\n' ' '|jq '.data.volumes[0].chapters[0].id'|sed 's/\"//g'`
                  fi
           
	          #获取这本书(bookId)的第一章的内容
                  contentUrl="curl -H 'appid: 101' -H 'terminal: aphone' -H 'version: 0.5.1' -H 'market: offical' -H 'model: OPPO+A37m' -H 'os: 5.1' -H 'sdk: 22'  -H 'User-Agent: okhttp/3.11.0' --compressed \"http://$env/v1/book/chapter/content?book=$bookId&source=$sourId&chapter=$chapterId&auid=5ab14cc165b4f075b5014001f6010f9f&uid=0&ver=0.5.1&pl=1&ts=1560829697167&chl=offical&tk=&_r=859152654051&sig=3ea82d6e030ed9f46213053d342f18ea\""
	          contentResult=`eval $contentUrl`
                  sleep 1
		  code=`echo $contentResult|tr '\r\n' ' '|jq .code`
                  logger "chapterId:$chapterId,contentCode:$code,sourceName:$sourName" 
        
	          #判断如果获取内容失败，把该书的名称和id记录下来。 
              	  if [[ $code -ne 200 ]];then
                        logger "errorMessage_modulesUrl:$modulesUrl"
                        logger "errorMessage_detailRul:$detailUrl"
                        logger "errorMessage_detailResult:$sourU"
                        logger "errorMessage_chapterUrl:$chapterUrl"
                        logger "errorMessage_chapterResult:$chapterResult"
                        logger "errorMessage_contentUrl:$contentUrl" 
                        logger "errorMessage_contentResult:$contentResult" 
	                reContentResult=`eval $contentUrl`
                        logger "reContentResult:$reContentResult"
                        sleep 1
		        reCode=`echo $reContentResult|tr '\r\n' ' '|jq .code`
                        logger "recode:$reCode"
                        if [[ $reCode -ne 200 ]];then
		        arrayBook[${#arrayBook[*]}]="<tr><th>$bookId</th><th>$bookName</th><th>$channelsName</th><th>$sourName</th></tr>"
                        fi
		  else
	             content=`echo $contentResult|sed 's/{//g'|sed 's/}//g'|sed 's/\n//g'`
	             con=${content#*content}
	             length=`echo $con| wc -c`
	             echo length:$length
		     if [[ $length -lt 5 ]];then
                        logger "errorMessage_detailRul:$detailUrl"
                        logger "errorMessage_detailResult:$sourU"
                        logger "errorMessage_chapterUrl:$chapterUrl"
                        logger "errorMessage_chapterResult:$chapterResult"
                        logger "errorMessage_contentUrl:$contentUrl" 
                        logger "errorMessage_contentResult:$contentResult" 
		        logger "content内容长度小于5:content:$con" 
		        arrayBook[${#arrayBook[*]}]="<tr><th>$bookId</th><th>$bookName</th><th>$channelsName</th><th>$sourName</th></tr>"
                     fi            
                  fi 
	      break
              done
        #break
        #done
   break
   done 
  
   #判断数组长度，如果大于0，发邮件报警。
   logger "查询数据数量：$bookCount"  
   if (( ${#arrayBook[*]} > 100000000 ));then
       logger "errorBookMessage:${arrayBook[*]}"
       #recevier="yanbingquan@aggrx.com,chenjiannan@aggrx.com" 
       recevier="yanbingquan@aggrx.com" 
       subject="点阅书城频道书籍内容获取失败报警"
       text="<html lang=\"zh\">
         <head>
         <meta charset=\"UTF-8\"/>
         <title>channel-check</title>
         </head>
         <body>
	  <p>书籍章节详情数据</p>
	  <table border="1">
             <tr>
               <th>书籍ID</th>
               <th>书籍名称</th>
               <th>频道</th>
               <th>来源</th>
             </tr>
	  ${arrayBook[*]}
	  </body>
        </html>"
       emailEnv="xxxxxxxxx:8182"
       mailUrl="curl \"http://$emailEnv/api/mail/send\" -X POST  -d \"from=yanbingquan@aggrx.com&to=$recevier&subject=$subject&text=$text\""
       echo $mailUrl
       emailResult=`eval $mailUrl`
       logger "报警邮件:$emailResult"
   fi

